Abstract Classes


Abstract

The advantage of the abstract is that it allows visualizing, generalizing and possibly give life to an idea that is still in the process of becoming. The result is that the knowledge is expanded. The disadvantage of the abstract is that it is difficult to understand, the scope is not known, and worst, the human does not know if this idea is useful or practical.
La ventaja de lo abstracto es que permite visualizar, generalizar y posiblemente dar vida a una idea que aún está en el proceso de ser. El resultado es que el conocimiento se expande. La desventaja de lo abstracto es que es difícil de comprender, no se sabe los alcances, incluso no se sabe si sea útil o práctico.

Practical (Specific)

The advantage of something practical is that it allows analyzing and evaluating an abstract idea taking the abstract to the specific. The disadvantage of the practical is that it does analyze new ideas or their scopes. The result is that the knowledge is limited.
La ventaja de lo práctico es que permite analizar y evaluar una idea abstracta llevando lo abstracto a lo específico. La desventaja de lo práctico es que limita una idea y sus alcances. El resultado es que el conocimiento se limita.

Pure Virtual Function

In some cases, a class needs to have a function without its implementation; these functions are called pure virtual functions. For instance, suppose you are creating a base class called Document with pure virtual function called Save(). And suppose also that another programmer, Henry, is creating a TextDocument class derived from your Document class. If Henry tries to create an object from his TextDocument class, the code will not compile because the Save() function is not implemented. In other words, pure virtual functions provide a way to describe the functions of a class allowing implementing these functions in the derived classes.
En algunos casos, una clase necesita tener una función sin su implementación; estas funciones son llamadas funciones virtuales puras. Por ejemplo, suponga que usted está creando una clase base llamada Document con una función virtual pura Save(). Y suponga también que otro programador, Enrique, está creando una clase TextDocument de su clase Document. Si Enrique trata de crear un objeto de su clase TextDocument, el código no compilará porque la función Save() no está implementada. En otras palabras, las funciones virtuales puras proveen una forma de describir las funciones de una clase permitiendo implementar estas funciones en la clase derivada.

Document.h
#pragma once
class Document
{
public:
     Document(void);
     ~Document(void);
     virtual void Save() = 0; // A PURE VIRTUAL FUNCTION
};

Document.cpp
#include "StdAfx.h"
#include "Document.h"

Document::Document(void)
{
}

Document::~Document(void)
{
}


TextDocument.h
#pragma once
#include "document.h"
class TextDocument : public Document
{
public:
     TextDocument(void);
     ~TextDocument(void);
     void Save();
};

TextDocument.cpp
#include "StdAfx.h"
#include "TextDocument.h"

TextDocument::TextDocument(void)
{
}

TextDocument::~TextDocument(void)
{
}

void TextDocument::Save()
{
     HANDLE file = ::CreateFile(L"data.csv", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
     const char* data = "one, two, three";
     DWORD bytesToWrite = strlen(data);
     DWORD written;
     ::WriteFile(file, &data, bytesToWrite, &written, NULL);
     ::CloseHandle(file);
}


Abstract Class

An abstract class is a class that has one or more pure virtual functions. It represents an abstract idea. An abstract class is always used to derive a new class. The derived class MUST implement the pure virtual functions of the base class, so that it can become a non-abstract class.
Una clase abstracta es una clase que tiene una o más funciones virtuales puras. Ésta representa una idea abstracta. Una clase abstracta es usada siempre para derivar una clase nueva. La clase derivada DEBE implementar las funciones virtuales puras de la clase base, para que esta pueda convertirse en una clase no abstracta.

Problem 2
Create a complete project that illustrates how to use abstract classes, you must design at least two classes.
Cree un proyecto completo que ilustre como usar las clases abstractas, usted debe diseñar al menos dos clases.

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home